function varargout = resizeGUI(varargin)
% RESIZEGUI MATLAB code for resizeGUI.fig
%      RESIZEGUI, by itself, creates a new RESIZEGUI or raises the existing
%      singleton*.
%
%      H = RESIZEGUI returns the handle to a new RESIZEGUI or the handle to
%      the existing singleton*.
%
%      RESIZEGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in RESIZEGUI.M with the given input arguments.
%
%      RESIZEGUI('Property','Value',...) creates a new RESIZEGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before resizeGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to resizeGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help resizeGUI

% Last Modified by GUIDE v2.5 20-Jan-2012 22:14:13

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @resizeGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @resizeGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before resizeGUI is made visible.
function resizeGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to resizeGUI (see VARARGIN)

% Choose default command line output for resizeGUI
handles.output = hObject;
    
    handles.im = varargin{1};
    handles.ax = varargin{2};
    handles.label = varargin{3};
    set(handles.labelWidth,'Visible','off');
    set(handles.labelHeight,'Visible','off');
    set(handles.textEditWidth,'Visible','off');
    set(handles.textEditHeight,'Visible','off');
    set(handles.keepRatio,'Visible','off');

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes resizeGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = resizeGUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function textEditWidth_Callback(hObject, eventdata, handles)
% hObject    handle to textEditWidth (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of textEditWidth as text
%        str2double(get(hObject,'String')) returns contents of
%        textEditWidth as a double
    if get(handles.keepRatio,'Value') == 1
        dim = size(handles.im);
        ar = dim(2) / dim(1);
        h = str2num(get(handles.textEditWidth,'String')) / ar; 
        set(handles.textEditHeight,'String',num2str(h));
    end
        
% --- Executes during object creation, after setting all properties.
function textEditWidth_CreateFcn(hObject, eventdata, handles)
% hObject    handle to textEditWidth (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function textEditHeight_Callback(hObject, eventdata, handles)
% hObject    handle to textEditHeight (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of textEditHeight as text
%        str2double(get(hObject,'String')) returns contents of textEditHeight as a double
    if get(handles.keepRatio,'Value') == 1
        dim = size(handles.im);
        ar = dim(2) / dim(1);
        w = str2num(get(handles.textEditHeight,'String')) * ar; 
        set(handles.textEditWidth,'String',num2str(w));
    end

% --- Executes during object creation, after setting all properties.
function textEditHeight_CreateFcn(hObject, eventdata, handles)
% hObject    handle to textEditHeight (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in keepRatio.
function keepRatio_Callback(hObject, eventdata, handles)
% hObject    handle to keepRatio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of keepRatio


% --- Executes on button press in cancelButton.
function cancelButton_Callback(hObject, eventdata, handles)
% hObject    handle to cancelButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    close resizeGUI;


% --- Executes on button press in okButton.
function okButton_Callback(hObject, eventdata, handles)
% hObject    handle to okButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    if get(handles.radioPx,'Value') == 1
        imgWidth = str2num(get(handles.textEditWidth,'String'));
        imgHeight = str2num(get(handles.textEditHeight,'String'));
        handles.im = imresize(handles.im,[imgHeight imgWidth]);
    elseif get(handles.radioPercent,'Value') == 1
        scale = str2num(get(handles.editTextPercent,'String')) / 100;
        handles.im = imresize(handles.im, scale);
    end
    axes(handles.ax);
    imshow(handles.im);
    axis off;
    axis image;
    s = size(handles.im);
    strsize = strcat('(',num2str(s(2)),'x',num2str(s(1)), ')');
    set(handles.label,'String',strsize);
    guidata(hObject, handles);
    close resizeGUI;



function editTextPercent_Callback(hObject, eventdata, handles)
% hObject    handle to editTextPercent (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editTextPercent as text
%        str2double(get(hObject,'String')) returns contents of editTextPercent as a double


% --- Executes during object creation, after setting all properties.
function editTextPercent_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editTextPercent (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
    


% --- Executes when selected object is changed in resizeOptions.
function resizeOptions_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in resizeOptions 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
    if get(handles.radioPx,'Value') == 1
        set(handles.labelWidth,'Visible','on');
        set(handles.labelHeight,'Visible','on');
        set(handles.textEditWidth,'Visible','on');
        set(handles.textEditHeight,'Visible','on');
        set(handles.keepRatio,'Visible','on');
        set(handles.editTextPercent,'Visible','off');
        set(handles.labelPercent,'Visible','off');
    elseif get(handles.radioPercent,'Value') == 1
        set(handles.labelWidth,'Visible','off');
        set(handles.labelHeight,'Visible','off');
        set(handles.textEditWidth,'Visible','off');
        set(handles.textEditHeight,'Visible','off');
        set(handles.keepRatio,'Visible','off');
        set(handles.editTextPercent,'Visible','on');
        set(handles.labelPercent,'Visible','on');
    end
        
